﻿using System;

/*
Write a program that reads a positive integer number N (N < 20) 
from console and outputs in the console the numbers 1 ... N 
numbers arranged as a spiral.
*/

class PrintSnailMatrix
{
    static void Main(string[] args)
    {
        Console.Write("Enter N: ");
        int n = Int32.Parse(Console.ReadLine());

        int[,] arrayToFill = new int[n, n];

        // fillArray
        for (int i = 0; i < arrayToFill.GetLength(0); i++)
        {
            for (int j = 0; j < arrayToFill.GetLength(1); j++)
            {
                arrayToFill[i, j] = 0;
            }
        }

        int currentNumber = 1;

        for (int currentRow = 0, currentColumn = 0, maxRow = n - 1, maxColumn = n - 1; currentNumber <= n * n; currentRow++, currentColumn++, maxRow--,maxColumn--)
        {
            for (int row = currentRow, column = currentColumn; column < maxColumn; column++)
            {
                arrayToFill[row, column] = currentNumber;
                currentNumber++;
            }

            for (int row = currentRow, column = maxColumn ; row < maxRow ; row++)
            {
                arrayToFill[row, column] = currentNumber;
                currentNumber++;
            }

            for (int row = maxRow, column = maxColumn; column > currentColumn; column--)
            {
                arrayToFill[row, column] = currentNumber;
                currentNumber++;
            }

            for (int row = maxRow, column = currentColumn; row > currentRow; row--)
            {
                arrayToFill[row, column] = currentNumber;
                currentNumber++;
            }
            if (currentNumber == (n * n) && currentRow == maxRow && currentColumn == maxColumn)
            {
                arrayToFill[currentRow, currentColumn] = currentNumber;
                currentNumber++;
            }

        }
        


        // printArray
        for (int i = 0; i < arrayToFill.GetLength(0); i++)
        {
            for (int j = 0; j < arrayToFill.GetLength(1); j++)
            {
                Console.Write("{0,3} ", arrayToFill[i, j]);
            }
            Console.WriteLine("");
        }
    }
}

